package parser

import (
	"regexp"
	"engine"
	"fmt"
	"github.com/garyburd/redigo/redis"
)

var categoryRe = regexp.MustCompile(`<h2><a href="\/([a-z\/]+news[a-z\/]+)\/" title="[^\"]+" target="_blank">([^\"]+)</a></h2>`)

func ParseCategory(contents []byte) engine.ParseResult {
	matches := categoryRe.FindAllSubmatch(contents, -1)

	result := engine.ParseResult{}
	c, err := redis.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		fmt.Println("Connect to redis error", err)
	}else {
		fmt.Println("Connect to redis")
	}
	defer c.Close()
	for _, m := range matches {
		//判断url是否已经处理了
		url := "http://www.pusa123.com/" + string(m[1])
		ret, err := redis.Int(c.Do("SISMEMBER", "visitedUrls", url))
		if err != nil {
			fmt.Println("redis get failed: ", err)
		} else {
			//fmt.Println(ret)
			if ret == 1 {
				fmt.Println("Visited url: ",url)
				continue
			}else{
				fmt.Println("New url: ",url)
				_, err = c.Do("SADD", "visitedUrls", url)
				if err != nil {
					fmt.Println("redis set failed:", err)
				}
				result.Items = append(result.Items, "Category " + string(m[2]))
				result.Requests = append(result.Requests, engine.Request{
					Url:url,
					ParserFunc:func (c []byte) engine.ParseResult{
						return ParseList(c,url)
					}})
			}
		}

	}
	return result
}